<?php
include("data/yingying/function.php");
include("data/yingying/cURL.php");
use YingApi\Curl;
class YingApi {
	private $upload_api;
	private $submit_api;
	private $upload_url;
	public function __construct() {
		$this->upload_api="https://mediaplatform2.xiaoice.com/image/ExUpload";
		$this->submit_api="https://ux.xiaoice.com/api/imageAnalyze/Process?service=beauty";
	}
	public function upload($url) {
		$data=@file_get_contents($url);
		if(empty($data)) {
			Back(["code"=>203,"message"=>"图片资源已失效，请更换链接！"]);
			die();
		}
		$time=time().time();
		file_put_contents("data/yingying/png/".$time.".png",$data);
		$obj=Curl::ua("okhttp/3.12.10")->referer("https://appservice.qq.com/1111185308/1.0.66/page-frame.html")->upload($this->upload_api,[
		"file"=>new \CURLFile("data/yingying/png/".$time.".png")
		])->obj();
		unlink("data/yingying/png/".$time.".png");
		$this->upload_url=$obj->Host.$obj->Url;
		return $this;
	}
	public function check() {
		$json='{"MsgId":'.time().',"TraceId":"'.$this->uuid().'","CreateTime":'.time().',"Content":{"imageUrl":"'.$this->upload_url.'"}}';
		$obj=Curl::ua("Mozilla/5.0 (Linux; Android 9; Redmi 6 Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/80.0.3987.99 Mobile Safari/537.36 QQ/8.8.33.6300 V1_AND_SQ_8.8.33_2150_YYB_D QQ/MiniApp")->referer("https://appservice.qq.com/1111185308/1.0.66/page-frame.html")->header([
		"appreferer"=>"/QQXiaoiceSkills",
		"impressionid"=>"3bb36a8f-5593-4a53-8956-c0cfab6911f8"
		])->cookie("pname=;subpid=qqmp;uidcode=gBTYvVAUxdf3ke2yXsgNwitsz_LB4P3RM0hZmJf6yTW7hhcQHR_FFwmUKOKicR3X6e0eUR4VabMWxtnMg4YWcHJnlf0SgmWFA4rwgKV5aB-Ge1Ak2MvOpd75s9Akydc3LrmR6L25zw")->json($this->submit_api,$json)->obj();
		if($obj->content->metadata->face_number==0) {
			return ["code"=>201,"message"=>$obj->content->text];
		} else {
			return ["code"=>200,"message"=>$obj->content->text,"url"=>$obj->content->metadata->reportImgUrl,"score"=>$obj->content->metadata->score,"data"=>[
			$obj->content->metadata->FBR_Key0."：".$obj->content->metadata->FBR_Score0,
			$obj->content->metadata->FBR_Key1."：".$obj->content->metadata->FBR_Score1,
			$obj->content->metadata->FBR_Key2."：".$obj->content->metadata->FBR_Score2
			]];
		}
	}
	public function uuid() {
		$chars=md5(uniqid(mt_rand(),true));
		$uuid=substr($chars,0,8).'-'.substr($chars,8,4).'-'.substr($chars,12,4).'-'.substr($chars,16,4).'-'.substr($chars,20,12);
		return $uuid;
	}
	public function GetHttpStatusCode($url) {
		$curl=curl_init();
		curl_setopt($curl,CURLOPT_URL,$url);
		curl_setopt($curl,CURLOPT_HEADER,1);
		curl_setopt($curl,CURLOPT_NOBODY,1);
		curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($curl,CURLOPT_TIMEOUT,30);
		curl_exec($curl);
		$rtn=curl_getinfo($curl,CURLINFO_HTTP_CODE);
		curl_close($curl);
		return $rtn;
	}
}
$image=$_REQUEST["url"];//需要查看的图片直链
if(empty($image)) {
	Back(["code"=>202,"message"=>"请输入图片链接！"]);
	die();
}
$Api=new YingApi();
Back($Api->upload($image)->check());
die();